home *** CD-ROM | disk | FTP | other *** search
/ The PC-SIG Library 10 / The PC-Sig Library - Shareware for the IBM PC and Compatibles (PC-SIG)(Tenth Edition Disks 1-2804)(1991).iso / PC_SIGCD / 22 / 4 / DISK2247.ZIP / CBASE101.ZIP / CBASE.ZIP / CBIMP.C < prev    next >
Text File  |  1990-06-21  |  10KB  |  546 lines

  1. /*    Copyright (c) 1989 Citadel    */
  2. /*       All Rights Reserved        */
  3.  
  4. /* #ident    "@(#)cbimp.c    1.4 - 90/06/20" */
  5.  
  6. /* ansi headers */
  7. #include <ctype.h>
  8. #include <errno.h>
  9. #include <limits.h>
  10. #include <stdio.h>
  11. /*#include <stddef.h>*/
  12. /*#include <string.h>*/
  13.  
  14. /* local headers */
  15. #include "cbase_.h"
  16.  
  17. #ifndef isodigit
  18. #define isodigit(c)    (((c) >= '0' && (c) <= '7') ? 1 : 0)
  19. #endif
  20.  
  21. /*man---------------------------------------------------------------------------
  22. NAME
  23.      cbimp - import cbase data
  24.  
  25. SYNOPSIS
  26.  
  27. DESCRIPTION
  28.  
  29. SEE ALSO
  30.      cbcmp, cbexp.
  31.  
  32. DIAGNOSTICS
  33.      Upon successful completion, a value of 0 is returned.  Otherwise,
  34.      a value of -1 is returned, and errno set to indicate the error.
  35.  
  36. ------------------------------------------------------------------------------*/
  37. /* array data type import macro */
  38. #define vimp(IMPFCT) {                            \
  39.     int i = 0;                            \
  40.     int nelems = n / sizeof(*cp);                    \
  41.                                     \
  42.     for (i = 0; i < nelems; ++i) {                    \
  43.         if (IMPFCT(fp, cp, sizeof(*cp)) == -1) return -1;    \
  44.         ++cp;                            \
  45.     }                                \
  46.     return 0;                            \
  47. }
  48.  
  49. #define MAXFLDLEN    (1024)
  50. static char s[MAXFLDLEN + 1];
  51.  
  52. /* getfld:  get next field from import file */
  53. static int getfld(fp)
  54. FILE *fp;
  55. {
  56.     int i = 0;
  57.     int c = 0;
  58.  
  59.     memset(s, 0, sizeof(s));
  60.     for (i = 0; ; ++i) {
  61.         c = fgetc(fp);
  62.         if (ferror(fp)) {
  63.             return -1;
  64.         }
  65.         if (feof(fp)) {
  66.             break;
  67.         }
  68.         if (c == EXPFLDDLM || c == EXPRECDLM) {
  69.             break;
  70.         }
  71.         if (i >= sizeof(s) - 1) {
  72.             return -1;
  73.         }
  74.         s[i] = c;
  75.     }
  76.     s[i] = NUL;
  77.  
  78.     return 0;
  79. }
  80.  
  81. /* t_char -> use t_uchar import function */
  82. #define charimp        ucharimp
  83.  
  84. /* t_charv -> use t_ucharv import function */
  85. #define charvimp    ucharvimp
  86.  
  87. /* ucharimp:  t_uchar import function */
  88. static int ucharimp(fp, p, n)
  89. FILE *fp;
  90. const void *p;
  91. size_t n;
  92. {
  93.     int c = 0;
  94.  
  95.     if (getfld(fp) == -1) {
  96.         return -1;
  97.     }
  98.     c = (unsigned char)s[0];
  99.     if (c == EXPESC) {
  100.         switch ((unsigned char)s[1]) {
  101.         case NUL:    /* premature end of field */
  102.             return -1;
  103.             break;    /* case NUL: */
  104. #if __STDC__ == 1
  105.         case 'a':    /* audible alert */
  106.             c = '\a';
  107.             break;    /* case 'a': */
  108. #endif
  109.         case 'b':    /* backspace */
  110.             c = '\b';
  111.             break;    /* case 'b': */
  112.         case 'f':    /* form feed */
  113.             c = '\f';
  114.             break;    /* case 'f': */
  115.         case 'n':    /* newline */
  116.             c = '\n';
  117.             break;    /* case 'n': */
  118.         case 'r':    /* carriage return */
  119.             c = '\r';
  120.             break;    /* case 'r': */
  121.         case 't':    /* horizontal tab */
  122.             c = '\t';
  123.             break;    /* case 't': */
  124.         case 'v':    /* vertical tab */
  125.             c = '\v';
  126.             break;    /* case 'v': */
  127.         default:    /* */
  128.             if (isodigit(s[1])) {
  129.                 if (!isodigit(s[2]) || !isodigit(s[3])) return -1;
  130.                 if (sscanf(s, "%3o", &c) != 1) return -1;
  131.             } else {
  132.                 c = (unsigned char)s[1];
  133.             }
  134.             break;    /* default: */
  135.         }
  136.     }
  137.     *(unsigned char *)p = c;
  138.  
  139.     return 0;
  140. }
  141.  
  142. /* ucharvimp:  t_ucharv import function */
  143. static int ucharvimp(fp, p, n)
  144. FILE *fp;
  145. const void *p;
  146. size_t n;
  147. {
  148.     unsigned char *cp = (unsigned char *)p;
  149.  
  150.     vimp(ucharimp);
  151. }
  152.  
  153. /* shortimp:  t_short import function */
  154. static int shortimp(fp, p, n)
  155. FILE *fp;
  156. const void *p;
  157. size_t n;
  158. {
  159.     if (getfld(fp) == -1) {
  160.         return -1;
  161.     }
  162.     if (sscanf(s, "%hd", (short *)p) != 1) return -1;
  163.  
  164.     return 0;
  165. }
  166.  
  167. /* shortvimp:  t_shortv import function */
  168. static int shortvimp(fp, p, n)
  169. FILE *fp;
  170. const void *p;
  171. size_t n;
  172. {
  173.     signed short *cp = (signed short *)p;
  174.  
  175.     vimp(shortimp);
  176. }
  177.  
  178. /* ushortimp:  t_ushort import function */
  179. static int ushortimp(fp, p, n)
  180. FILE *fp;
  181. const void *p;
  182. size_t n;
  183. {
  184.     if (getfld(fp) == -1) {
  185.         return -1;
  186.     }
  187.     if (sscanf(s, "%hu", (unsigned short *)p) != 1) return -1;
  188.  
  189.     return 0;
  190. }
  191.  
  192. /* ushortvimp:  t_ushortv import function */
  193. static int ushortvimp(fp, p, n)
  194. FILE *fp;
  195. const void *p;
  196. size_t n;
  197. {
  198.     unsigned short *cp = (unsigned short *)p;
  199.  
  200.     vimp(ushortimp);
  201. }
  202.  
  203. /* intimp:  t_int import function */
  204. static int intimp(fp, p, n)
  205. FILE *fp;
  206. const void *p;
  207. size_t n;
  208. {
  209.     if (getfld(fp) == -1) {
  210.         return -1;
  211.     }
  212.     if (sscanf(s, "%d", (int *)p) != 1) return -1;
  213.  
  214.     return 0;
  215. }
  216.  
  217. /* intvimp:  t_intv import function */
  218. static int intvimp(fp, p, n)
  219. FILE *fp;
  220. const void *p;
  221. size_t n;
  222. {
  223.     signed int *cp = (signed int *)p;
  224.  
  225.     vimp(intimp);
  226. }
  227.  
  228. /* uintimp:  t_uint import function */
  229. static int uintimp(fp, p, n)
  230. FILE *fp;
  231. const void *p;
  232. size_t n;
  233. {
  234.     if (getfld(fp) == -1) {
  235.         return -1;
  236.     }
  237.     if (sscanf(s, "%u", (unsigned int *)p) != 1) return -1;
  238.  
  239.     return 0;
  240. }
  241.  
  242. /* uintvimp:  t_uintv import function */
  243. static int uintvimp(fp, p, n)
  244. FILE *fp;
  245. const void *p;
  246. size_t n;
  247. {
  248.     unsigned int *cp = (unsigned int *)p;
  249.  
  250.     vimp(uintimp);
  251. }
  252.  
  253. /* longimp:  t_long import function */
  254. static int longimp(fp, p, n)
  255. FILE *fp;
  256. const void *p;
  257. size_t n;
  258. {
  259.     if (getfld(fp) == -1) {
  260.         return -1;
  261.     }
  262.     if (sscanf(s, "%ld", (long *)p) != 1) return -1;
  263.  
  264.     return 0;
  265. }
  266.  
  267. /* longvimp:  t_longv import function */
  268. static int longvimp(fp, p, n)
  269. FILE *fp;
  270. const void *p;
  271. size_t n;
  272. {
  273.     signed long *cp = (signed long *)p;
  274.  
  275.     vimp(longimp);
  276. }
  277.  
  278. /* ulongimp:  t_ulong import function */
  279. static int ulongimp(fp, p, n)
  280. FILE *fp;
  281. const void *p;
  282. size_t n;
  283. {
  284.     if (getfld(fp) == -1) {
  285.         return -1;
  286.     }
  287.     if (sscanf(s, "%lu", (unsigned long *)p) != 1) return -1;
  288.  
  289.     return 0;
  290. }
  291.  
  292. /* ulongvimp:  t_ulongv import function */
  293. static int ulongvimp(fp, p, n)
  294. FILE *fp;
  295. const void *p;
  296. size_t n;
  297. {
  298.     unsigned long *cp = (unsigned long *)p;
  299.  
  300.     vimp(ulongimp);
  301. }
  302.  
  303. /* floatimp:  t_float import function */
  304. static int floatimp(fp, p, n)
  305. FILE *fp;
  306. const void *p;
  307. size_t n;
  308. {
  309.     if (getfld(fp) == -1) {
  310.         return -1;
  311.     }
  312.     if (sscanf(s, "%g", (float *)p) != 1) return -1;
  313.  
  314.     return 0;
  315. }
  316.  
  317. /* floatvimp:  t_floatv import function */
  318. static int floatvimp(fp, p, n)
  319. FILE *fp;
  320. const void *p;
  321. size_t n;
  322. {
  323.     float *cp = (float *)p;
  324.  
  325.     vimp(floatimp);
  326. }
  327.  
  328. /* dblimp:  t_ldouble import function */
  329. static int dblimp(fp, p, n)
  330. FILE *fp;
  331. const void *p;
  332. size_t n;
  333. {
  334.     if (getfld(fp) == -1) {
  335.         return -1;
  336.     }
  337.     if (sscanf(s, "%lg", (double *)p) != 1) return -1;
  338.  
  339.     return 0;
  340. }
  341.  
  342. /* dblvimp:  t_doublev import function */
  343. static int dblvimp(fp, p, n)
  344. FILE *fp;
  345. const void *p;
  346. size_t n;
  347. {
  348.     double *cp = (double *)p;
  349.  
  350.     vimp(dblimp);
  351. }
  352.  
  353. /* ldblimp:  t_ldouble import function */
  354. static int ldblimp(fp, p, n)
  355. FILE *fp;
  356. const void *p;
  357. size_t n;
  358. {
  359.     if (getfld(fp) == -1) {
  360.         return -1;
  361.     }
  362. #ifdef AC_LDOUBLE
  363.     if (sscanf(s, "%Lg", (long double *)p) != 1) return -1;
  364. #endif
  365.     return 0;
  366. }
  367.  
  368. /* ldblimp:  t_ldouble import function */
  369. static int ldblvimp(fp, p, n)
  370. FILE *fp;
  371. const void *p;
  372. size_t n;
  373. {
  374. #ifdef AC_LDOUBLE
  375.     long double *cp = (long double *)p;
  376. #else
  377.     double *cp = (double *)p;
  378. #endif
  379.     vimp(ldblimp);
  380. }
  381.  
  382. /* ptrimp:  t_pointer import function */
  383. static int ptrimp(fp, p, n)
  384. FILE *fp;
  385. const void *p;
  386. size_t n;
  387. {
  388.     if (getfld(fp) == -1) {
  389.         return -1;
  390.     }
  391.     if (sscanf(s, "%p", (void **)p) != 1) return -1;
  392.  
  393.     return 0;
  394. }
  395.  
  396. /* ptrvimp:  t_pointerv import function */
  397. static int ptrvimp(fp, p, n)
  398. FILE *fp;
  399. const void *p;
  400. size_t n;
  401. {
  402.     void **cp = (void **)p;
  403.  
  404.     vimp(ptrimp);
  405. }
  406.  
  407. /* strimp:  t_string import function */
  408. static int strimp(fp, p, n)
  409. FILE *fp;
  410. const void *p;
  411. size_t n;
  412. {
  413.     unsigned char *si = NULL;
  414.     int i = 0;
  415.     int c = 0;
  416.  
  417.     if (getfld(fp) == -1) {
  418.         return -1;
  419.     }
  420.  
  421.     /* initialize return */
  422.     memset(p, 0, n);
  423.  
  424.     /* convert */ /* string will be truncated if too long */
  425.     si = (unsigned char *)s;
  426.     for (i = 0; i < n - 1; ++i) {
  427.         c = *si++;
  428.         if (c == NUL) {
  429.             *((char *)p + i) = NUL;
  430.             break;
  431.         }
  432.         if (c == EXPESC) {
  433.             switch (*si) {
  434.             case NUL:    /* premature end of field */
  435.                 return -1;
  436.                 break;    /* case NUL: */
  437. #if __STDC__ == 1
  438.             case 'a':    /* audible alert */
  439.                 c = '\a';
  440.                 break;    /* case 'a': */
  441. #endif
  442.             case 'b':    /* backspace */
  443.                 c = '\b';
  444.                 break;    /* case 'b': */
  445.             case 'f':    /* form feed */
  446.                 c = '\f';
  447.                 break;    /* case 'f': */
  448.             case 'n':    /* newline */
  449.                 c = '\n';
  450.                 break;    /* case 'n': */
  451.             case 'r':    /* carriage return */
  452.                 c = '\r';
  453.                 break;    /* case 'r': */
  454.             case 't':    /* horizontal tab */
  455.                 c = '\t';
  456.                 break;    /* case 't': */
  457.             case 'v':    /* vertical tab */
  458.                 c = '\v';
  459.                 break;    /* case 'v': */
  460.             default:    /* */
  461.                 if (isodigit(*si)) {
  462.                     if (!isodigit(*(si + 1)) || !isodigit(*(si + 2))) return -1;
  463.                     if (sscanf(si, "%3o", &c) != 1) return -1;
  464.                     si += 2;
  465.                 } else {
  466.                     c = *si;
  467.                 }
  468.                 break;    /* default: */
  469.             }
  470.             ++si;
  471.         }
  472.         *((char *)p + i) = c;
  473.     }
  474.     *((char *)p + n - 1) = NUL;
  475.  
  476.     return 0;
  477. }
  478.  
  479. /* t_cistring -> use t_string import function */
  480. #define cistrimp    strimp
  481.  
  482. /* binimp:  t_binary import function */
  483. static int binimp(fp, p, n)
  484. FILE *fp;
  485. const void *p;
  486. size_t n;
  487. {
  488.     /* calculate number of hex digits for each char
  489.          # bits in char == CHAR_BIT, # bits in hex digit == 4 */
  490.     const int hexdigits = (CHAR_BIT + (4 - 1)) / 4;
  491.     unsigned char *si = (unsigned char *)s;
  492.     unsigned char *pi = (unsigned char *)p;
  493.     char fmt[24];            /* printf format string */
  494.     int c = 0;
  495.  
  496.     sprintf(fmt, "%%%dX", hexdigits);
  497.  
  498.     if (getfld(fp) == -1) {
  499.         return -1;
  500.     }
  501.  
  502.     /* initialize return */
  503.     memset(p, 0, n);
  504.  
  505.     while (1) {
  506.         if (si - (unsigned char *)s >= sizeof(s) - hexdigits) break;
  507.         if (pi - (unsigned char *)p >= n) break;
  508.         if (sscanf(si, fmt, &c) != 1) return -1;
  509.         si += hexdigits;
  510.         *pi++ = (unsigned char)c;
  511.     }
  512.  
  513.     return 0;
  514. }
  515.  
  516. /* import function table definition */
  517. const cbimp_t cbimpv[] = {
  518.     charimp,        /* t_char    =  0 */
  519.     charvimp,        /* t_charv    =  1 */
  520.     ucharimp,        /* t_uchar    =  2 */
  521.     ucharvimp,        /* t_ucharv    =  3 */
  522.     shortimp,        /* t_short    =  4 */
  523.     shortvimp,        /* t_shortv    =  5 */
  524.     ushortimp,        /* t_ushort    =  6 */
  525.     ushortvimp,        /* t_ushortv    =  7 */
  526.     intimp,            /* t_int    =  8 */
  527.     intvimp,        /* t_intv    =  9 */
  528.     uintimp,        /* t_uint    = 10 */
  529.     uintvimp,        /* t_uintv    = 11 */
  530.     longimp,        /* t_long    = 12 */
  531.     longvimp,        /* t_longv    = 13 */
  532.     ulongimp,        /* t_ulong    = 14 */
  533.     ulongvimp,        /* t_ulongv    = 15 */
  534.     floatimp,        /* t_float    = 16 */
  535.     floatvimp,        /* t_floatv    = 17 */
  536.     dblimp,            /* t_double    = 18 */
  537.     dblvimp,        /* t_doublev    = 19 */
  538.     ldblimp,        /* t_ldouble    = 20 */
  539.     ldblvimp,        /* t_ldoublev    = 21 */
  540.     ptrimp,            /* t_pointer    = 22 */
  541.     ptrvimp,        /* t_pointerv    = 23 */
  542.     strimp,            /* t_string    = 24 */
  543.     cistrimp,        /* t_cistring    = 25 */
  544.     binimp            /* t_binary    = 26 */
  545. };
  546.